<?php
/*
 * 视频关键词匹配
 * 检查表
 *      xd_xxx,
 *      xd_xxx_data
 *      xx/201307/31/
 *      xxx/SpiderResult.db3
 * @package    system
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 60a38e651d5e41e3a731787b4d70e4c1d40a7dab $
 */
set_time_limit(0);
include '../../common.inc.php';
$hd = new ImportLoop();
$hd->setAim('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$hd->setSource('mysql:host=192.168.1.19;dbname=ixdcw', 'root', 'root');
$hd->setSourceInfo('xd_video', 'itemid', ' status=2 and videohandle=0');
$params = array(
	'maxid'     => isset($_GET['maxid']) ? $_GET['maxid'] : 0,
	'id'        => isset($_GET['id']) && !empty($_GET['id']) ? $_GET['id'] : 0,
	'startTime' => isset($_GET['startTime']) ? $_GET['startTime'] : time(),
	'limit'     => 150,
	'interval'  => 0,
	'debug'     => '',
);

$hd->initParams($params);

$sql = 'select * from ' . $hd->getSourceTable() . ' where itemid>' . $hd->getParam('id') . ' and status=2 and videohandle=0 order by itemid ASC limit ' . $hd->getParam('limit');

$result = $hd->getDbSource()->query($sql);
$rs     = $result->fetchAll();

if (!$rs) {
	$hd->end();
} else {
	foreach ($rs as $k => $v) {
		$insert = array();
		$hd->setParam('id', $v['itemid']); // set id

		$title = $v['title'];
		$ids   = AutoImport::match($title);
		$update['videohandle'] = 1;
		if ($ids[0] == 0) {
			$sqlUpdate = Sql::update($hd->getSourceTable(), $update, "itemid={$v['itemid']}");
			$hd->getDbAim()->query($sqlUpdate);
			$hd->times("当前处理ID:{$v['itemid']}, 跳过:未匹配到的项目<br>", 'warn');
			continue;
		} else {
			$update['arbid']       = $ids[0];
			$update['armid']       = $ids[1];
			$update['arcid']       = $ids[2];
			$update['status']      = 3;
			$sqlUpdate             = Sql::update($hd->getSourceTable(), $update, "itemid={$v['itemid']}");
			var_dump($sqlUpdate);
			if ($hd->getDbAim()->query($sqlUpdate)) {
				$hd->times("当前处理ID:{$v[$v['itemid']]}, 已经处理<br>", 'good');
			} else {
				exit($hd->times("当前处理ID:{$v['itemid']}, 处理失败<br>", 'error'));
			}

		}
	}
}
echo $hd->start($hd->getParams());